package ar.com.nexoweb.actualidadbasquet.adapters;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import ar.com.nexoweb.actualidadbasquet.R;
import ar.com.nexoweb.actualidadbasquet.entities.Noticia;


public class ListadoNoticiasAdapter extends ArrayAdapter<Noticia> {
	List<Noticia> imageAndTexts1 = null;

	public ListadoNoticiasAdapter(Activity activity,
			List<Noticia> imageAndTexts) {
		super(activity, 0, imageAndTexts);
		imageAndTexts1 = imageAndTexts;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();
		LayoutInflater inflater = activity.getLayoutInflater();

		View rowView = inflater.inflate(R.layout.adapter_basquetrosario_layout, null);
		TextView textView = (TextView) rowView.findViewById(R.id.feed_text);
		TextView timeFeedText = (TextView) rowView.findViewById(R.id.feed_updatetime);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.feed_image);
		try {

			Log.d("rssfeed", "imageAndTexts1.get(position).getImgLink() :: "
					+ imageAndTexts1.get(position).getImgLink() + " :: "
					+ imageAndTexts1.get(position).getTitle());
			textView.setText(imageAndTexts1.get(position).getTitle());
			
			String content = new String(imageAndTexts1.get(
					position).getDescription());

			timeFeedText.setText(content.subSequence(0, content.indexOf("-")));
			
			if (imageAndTexts1.get(position).getImgLink() != null) {

				URL feedImage = new URL(imageAndTexts1.get(position)
						.getImgLink().toString());
				if (!feedImage.toString().equalsIgnoreCase("null")) {
					HttpURLConnection conn = (HttpURLConnection) feedImage
							.openConnection();
					InputStream is = conn.getInputStream();
					Bitmap img = BitmapFactory.decodeStream(is);
					imageView.setImageBitmap(img);
				} else {
					imageView.setBackgroundResource(R.drawable.im);
				}
			}

		} catch (MalformedURLException e) {

		} catch (IOException e) {

		}

		return rowView;

	}

}